package com.le.tester.book.concurrentchallenge.threadSafedelegation;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * createTime：2025-03-17 9:39
 * description：当线程委托失效，使用了AtomicInteger线程安全的类，然后类在封装该线程安全类的取值，整个过程的set方法，变得不再是原子性的
 */
public class NumberRange {

    private final AtomicInteger lower = new AtomicInteger(0);

    private final AtomicInteger upper = new AtomicInteger(0);

    public void setLower(int i) {
        //不安全的变量，先检查再执行
        if (i > upper.get()) {
            throw new IllegalArgumentException();
        }
        lower.set(i);
    }

    public void setUpper(int i) {
        if (i < lower.get()) {
            throw new IllegalArgumentException();
        }
        upper.set(i);
    }

    public boolean isInRange(int i) {
        return (i >= lower.get() && i <= upper.get());
    }
}
